let userID = document.getElementById("userID")
let userFammilyName = document.getElementById("userFammilyName")
let userPassword = document.getElementById("userPassword")
let userEmail = document.getElementById("userEmail")
let userAddress = document.getElementById("userAddress")
let userSignUpTime = document.getElementById("userSignUpTime")
let userGivenName = document.getElementById("userGivenName")
window.addEventListener("load", (event) => {
    $.post("/user/catUserAccount",
        null,
        function (res) {
            if (res.status == "3001") {
                document.location = "/public/html/home.html"
            }
            else {
                userID.value=res.data[0].id
                userPassword.value=res.data[0].password
                userGivenName.value=res.data[0].given_name
                userAddress.value = res.data[0].address
                let date = new Date(res.data[0].signup_time*1000).toLocaleString()
                userSignUpTime.value=date
                userFammilyName.value=res.data[0].fammily_name
                userEmail.value=res.data[0].email
            }
        }, "json");

})
document.getElementById("userAccount").addEventListener("click",(event => {
    let parameter = {
        "id":userID.value,
        "fammily_name": userFammilyName.value,
        "given_name": userGivenName.value,
        "address": userAddress.value,
        "email": userEmail.value,
        "password": userPassword.value
    }
    $.post("/user/modifyUserAccount",
        parameter
        ,
        function (res) {
            if (res.status == "3001") {
                document.location = "/public/html/home.html"
            }
            else {
                alert(res.message)
                Cookies.set('username',userFammilyName.value.toString()+userGivenName.value.toString(),{ expires: 1 })
                location.reload()
            }

        }, "json");


}))